define(['jquery','api','user','util','text!pages/fund/charge.html','Vue'],function($,api,user,util){
	var vm,
	   backurl,
	   channels=[],
       userfunds={balance:0};
	   controller = {
	   init:function(url){
	   	   if(!user.checkLogin())return;
		   api.initFooter(3);
		   backurl=url;
		   api.setTitle('充值');
		   if(!vm){
		     this.render();
		     this.initChannel();
		   }else{
		     vm.show=true;
		   }
		   this.initFunds();
	   },
	   render:function(){
	   	   $('#pages').append('<div id="charge"></div>');
	       require(['text!pages/fund/charge.html','Vue'],function(tpl,Vue){
	            vm=new Vue({
	               el:'#charge',
	               data:{
	               	  show:true,
	               	  userfunds:userfunds,
	                  channels:channels,
	                  inChannel:'',
	                  bankInfo:[],
	                  inMoney:10,
	                  otherMoney:false,
	                  moneyType:[100, 500, 1000, 2000, 5000]
	               },
	               filters:{
	                  toStr:function(n){
	                 	 return String.fromCharCode(n);
	                  },
       	         	  priceFormat:function(v){
       	         	    return v?util.priceFormat(2,v):'';
       	         	  }
	               },
	               methods:{
	                  setMoney:function(n){
		                	this.inMoney=n;
		                	this.otherMoney=n=='';
		                },
		                toCharge:function(){
		                	var data={
		                	   amount:this.inMoney,
		                	   channel:this.inChannel,
		                	   type:51
		                	};
		                	if(this.inMoney==''||+this.inMoney<=0){
		                		msg('请选择或输入充值金额！');
		                	}else{
		                       api.charge(data,function(rs){
		                           if(rs.retcode=='0'){
		                              location.href=rs.urlcontent+'&backurl='+backurl;
		                           }
		                       });
		                	}
		                }
	               },
	               template:tpl
	            })
	       })
	   },
	   initFunds:function(){
	       api.userInfo(function(rs){
		   	   if(rs.retcode=='0'){
		   	   	  vm.userfunds=rs.userfund;
		   	   }else{
		   	      util.msg('用户资金信息加载失败!');
		   	   }
		   });
	   },
	   initChannel:function(){
	   	   var temp=[],
	   	       channel;
	   	   api.channels(function(rs){
	           if(rs.retcode=='0'){
	           	  for(var i=0;i<rs.data.length;i++){
	           	     channel=rs.data[i];
	           	     if(+channel.inmode>50)temp.push(channel);
	           	  }
	              vm.channels=temp;
	           }
	       })
	   },
	   onRouteChange:function(){
	       if(vm)vm.show=false;
	   }
	};
    return controller;
})